package com.hhang.boot.service.impl;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.hhang.boot.dao.ComDao;
import com.hhang.boot.exception.ServiceException;
import com.hhang.boot.service.ComService;
import com.hhang.boot.utils.ConstantUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Map;

@Service
public class ComServiceImpl implements ComService {

    @Autowired
    private ComDao comDao;
    @Override
    public int addGoods(Map<String, Object> param) {
        try {
            return comDao.addGoods(param);
        } catch (Exception e) {
            throw new RuntimeException("error_s数据提交失败",e);
        }
    }

    @Override
    public PageInfo<Map<String, Object>> findCom(Map<String, Object> param) throws ServiceException {
        try {
            //分页查询
            Object pObj = param.get("page");
            Object sObj = param.get("size");
            int page = 1;
            int size = ConstantUtil.SIZE;
            if (pObj != null) {
                page = Integer.parseInt((String) pObj);
                if (page < 1) {
                    page = 1;
                }
            }
            if (sObj != null) {
                size = Integer.parseInt((String) sObj);
            }
            PageHelper.startPage(page, size);
            List<Map<String, Object>> typeList = comDao.findCom(param);
            return new PageInfo<>(typeList);
        } catch (Exception e) {
            e.printStackTrace();
            throw new ServiceException("error_s服务查询异常", e);
        }
    }

    @Override
    public int delGoods(List<Long> ids) throws ServiceException {
        try {
            return comDao.delGoods(ids);
        } catch (Exception e) {
            e.printStackTrace();
            throw new ServiceException("删除失败", e);
        }
    }

    @Override
    public PageInfo<Map<String, Object>> findRepair(Map<String, Object> param) throws ServiceException {
        try {
            //分页查询
            Object pObj = param.get("page");
            Object sObj = param.get("size");
            int page = 1;
            int size = ConstantUtil.SIZE;
            if (pObj != null) {
                page = Integer.parseInt((String) pObj);
                if (page < 1) {
                    page = 1;
                }
            }
            if (sObj != null) {
                size = Integer.parseInt((String) sObj);
            }
            PageHelper.startPage(page, size);
            List<Map<String, Object>> petsList = comDao.findRepair(param);
            return new PageInfo<>(petsList);
        } catch (Exception e) {
            e.printStackTrace();
            throw new ServiceException("error_s服务查询异常", e);
        }
    }

    @Override
    public int addDuty(Map<String, Object> param) throws ServiceException {
        try {
            return comDao.addDuty(param);
        } catch (Exception e) {
            throw new RuntimeException("error_s数据提交失败",e);
        }
    }

    @Override
    public PageInfo<Map<String, Object>> findDuty(Map<String, Object> param) throws ServiceException {
        try {
            //分页查询
            Object pObj = param.get("page");
            Object sObj = param.get("size");
            int page = 1;
            int size = 22;
            if (pObj != null) {
                page = Integer.parseInt((String) pObj);
                if (page < 1) {
                    page = 1;
                }
            }
            if (sObj != null) {
                size = Integer.parseInt((String) sObj);
            }
            PageHelper.startPage(page, size);
            List<Map<String, Object>> typeList = comDao.findDuty(param);
            return new PageInfo<>(typeList);
        } catch (Exception e) {
            e.printStackTrace();
            throw new ServiceException("error_s服务查询异常", e);
        }
    }

    @Override
    public int delDuty(List<Long> ids) throws ServiceException {
        try {
            return comDao.delDuty(ids);
        } catch (Exception e) {
            e.printStackTrace();
            throw new ServiceException("删除失败", e);
        }
    }

    @Override
    public int upGoods(Map<String, Object> param) throws ServiceException {
        int res = -3;
        try {
            res = comDao.upGoods(param);
        } catch (Exception e) {
            e.printStackTrace();
            throw new ServiceException("服务异常，修改失败",e);
        }
        if (res == 0){
            throw new ServiceException("修改失败，请刷新重试");
        }
        return res;
    }

    @Override
    public int addRepair(Map<String, Object> param) throws ServiceException {
        try {
            //判断该账号该活动是否能被查询到即 是否报名过
                return comDao.addRepair(param);
        } catch (Exception e) {
            e.printStackTrace();
            throw new RuntimeException("error_s数据提交失败",e);
        }
    }

    @Override
    public int upRepair(Map<String, Object> param) throws ServiceException {
        int res = -2;
        try {
            res = comDao.upRepair(param);
        } catch (Exception e) {
            e.printStackTrace();
            throw new ServiceException("服务异常，修改失败",e);
        }
        if (res == 0){
            throw new ServiceException("修改失败，请刷新重试");
        }
        return res;
    }


}
